#include "service_impl.h"
#include "tools.h"

#include <vector>

using namespace std;

vector<Department*> departments;
vector<Employee*> employees;

ServiceImpl::ServiceImpl(void)
{
	m_pDao = new ServiceDaoFileImpl;
}
bool ServiceImpl::addDept(Department& department)
{
	m_pDao->load(departments);
	departments.push_back(&department);

	m_pDao->save(departments);
	return true;
}
bool ServiceImpl::deleteDept(int id)
{
	
	m_pDao->load(departments);

	if(!departments.size()) return false;

	vector<Department*>::iterator itor;	
    for (itor = departments.begin(); itor != departments.end(); itor++)
    {
        if((*itor)->getDept_id() == id)
        {       
            itor = departments.erase(itor);
            m_pDao->save(departments);
            return true;
        }
       
	}

	return false;
}
void ServiceImpl::listDept(void)
{
	m_pDao->load(departments);
	
	cout << "所有部门:" << endl;
	cout << "-------------------"<<endl;
	for(unsigned int i=0; i<departments.size(); i++)
	{
		cout<<departments.at(i)->getDept_id()<< " "<<departments.at(i)->getDept_name()<<" "<< departments.at(i)->getTotal()<<endl;
	}
	cout << "-------------------"<<endl;
}
bool ServiceImpl::addEmp(Employee& employee,int dept_id)
{
	m_pDao->load(departments);

	vector<Department*>::iterator itor;	
    for (itor = departments.begin(); itor != departments.end(); itor++)
    {
        if((*itor)->getDept_id() == dept_id)
        {   
        	(*itor)->addTotal();    
            (*itor)->get_employee_vector().push_back(&employee);
            m_pDao->save(departments);
            return true;
        }
       
	}

	return false;

}
bool ServiceImpl::deleteEmp(int _id)
{
	m_pDao->load(departments);

	if(!departments.size()) return false;

	vector<Department*>::iterator itor;	
    for (itor = departments.begin(); itor != departments.end(); itor++)
    {
    
        if((*itor)->deleteEmp(_id))
        {
        	(*itor)->subTotal(); 
        	m_pDao->save(departments);
        	return true;
        }
    }

	return false;


}
bool ServiceImpl::modifyEmp(Employee& employee)
{
	m_pDao->load(departments);

	if(!departments.size()) return false;

	vector<Department*>::iterator itor;	
    for (itor = departments.begin(); itor != departments.end(); itor++)
    {
    
        if((*itor)->modifyEmp(employee))
        {
        	m_pDao->save(departments);
        	return true;
        }
    }

	return false;

}
void ServiceImpl::listEmp(int id)
{
	m_pDao->load(departments);
	
	vector<Department*>::iterator itor;	
    for (itor = departments.begin(); itor != departments.end(); itor++)
    {
    
        if((*itor)->getDept_id() == id)
        {
        	employees.clear();
        	(*itor)->listEmp(employees);

			cout << "部门" << (*itor)->getDept_id() << "的所有员工:" << endl;
			cout << "-------------------"<<endl;
        	for(unsigned int i=0; i<employees.size(); i++)
        	{
        		cout << employees.at(i)->getId() << " " << employees.at(i)->getName() << " "<<employees.at(i)->getSex() 
        		<< " " << employees.at(i)->getAge() << endl;
        	}
			cout << "-------------------"<<endl;
        	return;
        }
    }
	
	cout << "该部门暂无员工!" <<endl;

}
void ServiceImpl::listAllEmp(void)
{
	m_pDao->load(departments);
	
	vector<Department*>::iterator itor;
	employees.clear();

    for (itor = departments.begin(); itor != departments.end(); itor++)
    {
    	(*itor)->listEmp(employees);
    }

	cout << "所有员工:"<< endl;
	cout << "-------------------"<<endl;
	for(unsigned int i=0; i<employees.size(); i++)
	{
		cout << employees.at(i)->getId() << " " << employees.at(i)->getName() << " "
		<<employees.at(i)->getSex() << " " << employees.at(i)->getAge() << endl;
	}
	cout << "-------------------"<<endl;
}

void ServiceImpl::myList(void)
{
	m_pDao->load(departments);
	
	vector<Department*>::iterator itor;
	employees.clear();

    for(itor = departments.begin(); itor != departments.end(); itor++)
    {
    	cout << "部门:"<< endl;
    	cout<<(*itor)->getDept_id()<< " "<<(*itor)->getDept_name()<<" "<< (*itor)->getTotal()<<endl;
		cout << "-------------------"<<endl;

		cout << "员工:"<<endl;
		employees.clear();
		(*itor)->listEmp(employees);
		for(unsigned int i=0; i<employees.size(); i++)
		{
			cout << employees.at(i)->getId() << " " << employees.at(i)->getName() << " "
			<<employees.at(i)->getSex() << " " << employees.at(i)->getAge() << endl;
		}
		
		cout << "-------------------"<<endl;
    	
    }

}

ServiceImpl::~ServiceImpl(void)
{
	delete []m_pDao;
}
